import {Body, Controller, HttpStatus, Post, Get, Res, Query} from '@nestjs/common';
import {Response} from 'express';
import {CommonResponseDto} from '../../dto/common.response.dto';
import {ApplyUserAuthType, ApplyUserStatus, ResCode} from '../../common/constants';
import {ApplyUserService} from "../../service/apply.user.service";
import {ApplyUserDto} from "../../dto/apply.user.dto";
import {uuid} from "../../common/utils";


@Controller('apply-admin/user')
export class ApplyUserController{
    constructor(private readonly applyUserService: ApplyUserService) {
    }

    @Post('/add')
    async addUser(@Body() applyUserDto: ApplyUserDto, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        if (!applyUserDto.name || !applyUserDto.accountId) {
            return new CommonResponseDto(ResCode.ERROR, 'name accountId 必填', null)
        }
        try {
            const user  = await this.applyUserService.getOneByAccountId(applyUserDto.accountId)
            if (user) {
                return new CommonResponseDto(ResCode.ERROR, 'accountId已存在', null)
            }
            applyUserDto.status = ApplyUserStatus.NORMAL
            applyUserDto.id = uuid(32)
            const result = await this.applyUserService.createOne(applyUserDto)
            return new CommonResponseDto(ResCode.OK, '成功', result)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/edit')
    async editUser(@Body() applyUserDto: ApplyUserDto, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        if (!applyUserDto.name || !applyUserDto.accountId) {
            return new CommonResponseDto(ResCode.ERROR, 'name accountId 必填', null)
        }
        try {
            if (![ApplyUserStatus.NORMAL, ApplyUserStatus.DELETED].includes(applyUserDto.status)) {
                return new CommonResponseDto(ResCode.ERROR, 'status状态不正确', null)
            }
            const user  = await this.applyUserService.getOneByAccountId(applyUserDto.accountId)
            if (user && user.id !== applyUserDto.id) {
                return new CommonResponseDto(ResCode.ERROR, 'accountId已存在', null)
            }
            const result = await this.applyUserService.editOne(applyUserDto)
            return new CommonResponseDto(ResCode.OK, '成功', result)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/edit-auth')
    async editUserAuth(@Body() body: {id: string, authType: ApplyUserAuthType}, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        if (!body || !body.authType) {
            return new CommonResponseDto(ResCode.ERROR, 'id authType 必填', null)
        }
        try {
            if (![ApplyUserAuthType.APPLY, ApplyUserAuthType.APPROVE, ApplyUserAuthType.ALL].includes(body.authType)) {
                return new CommonResponseDto(ResCode.ERROR, 'authType状态不正确', null)
            }
            const result = await this.applyUserService.editAuthType(body.id, body.authType)
            return new CommonResponseDto(ResCode.OK, '成功', result)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Get('/list')
    async getAllUserList(@Query() query: {status?: ApplyUserStatus}, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        try {
            const result = await this.applyUserService.getAll(query.status)
            return new CommonResponseDto(ResCode.OK, '成功', result)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }
}
